import type { PlasmoCSConfig } from "plasmo"
import { store } from "~store"
import { setToken, setIsInit } from '~store/controller/jinritemaiDecrypt'
import {
    getGoodsAsync, getDiscountActivity,
    getDetailNewUserGiftAllData,
    getCouponListAllData,
    getSurpriseTicketLisAllData,
    // getPromotionListFlashAllData,
    getDiscountOnePrdActivity
} from "~store/controller/jinritemaiProduct"
import { getshopFreightTemplateAsync, shopuserjwtSet, shopLogin, getUserShopID, setShopInfo, getUrlCookies } from "~store/controller/jinritemaiShops"
import { getShopInfoData } from "@Api/shopsApi"
import { Agreement } from "@Utils/addConfigFuncs"
import { getAutoSetCounterListStor, getProductList, getDlyUserBindInfos } from "~store/controller/autoCounps"
import { loginShopGetShopInfos, addNewConter } from '../../contents/jinritemai/componets/coupon/autoHome/autoCoupFunc'
import getAutoActivityListStor from './autoActivityInfoData'

export const config: PlasmoCSConfig = {
    matches: ["https://fxg.jinritemai.com/*"]
}
import { getOssKeyData } from '@Utils/getPageDecrypt'

function initPageInfo() {
    // console.log('initPageInfo', /\^*jinritemai\$*/.test(location.hostname))
    // getOssKeyData()

    if (/\^*jinritemai\$*/.test(location.hostname)) {
        // store.dispatch(getUrlCookies('')) // 保存cookies 作废
        // store.dispatch(getAutoSetCounterListStor())
    }
    function awtgetuserinfo() {
        if (location.pathname == '/' || location.pathname == '/login/common' || location.hostname !== 'fxg.jinritemai.com') {
            return
        }
        Agreement()

        // if ('fxg.jinritemai.com' === location.hostname) {
        // }
        // 判断登录
        return new Promise((resolve, reject) => {
            const jtwdata = localStorage.getItem('wtshopuser')
            if (jtwdata && jtwdata !== 'undefined') {
                // shopuserjwtSet
                try {
                    store.dispatch(shopuserjwtSet(JSON.parse(jtwdata)))
                    resolve(jtwdata)
                } catch (error) {
                    console.log('登录异常', error)
                    reject(false)
                    throw new Error('更改的非登录信息')
                }
            } else {
                console.log('初始化登录信息')

                // 调用登录接口
                store.dispatch(shopLogin((jwt) => {
                    console.log('初始化登录信息jwt0', jwt)

                    if (jwt) {
                        resolve(jwt)
                        return
                    }
                    reject(false)
                }))
            }
        })
    }

    setIndexFun()
    let setLoginIndex = setInterval(_ => {
        setIndexFun()
    }, 400)

    function setIndexFun() {
        const getdocjer: any = document.querySelector('.bSO6ZWFbUw8aFbrq6ZCd')
        if (getdocjer) {
            getdocjer.style.zIndex = 99
            clearInterval(setLoginIndex)
            setLoginIndex = 0
        }
    }
    setTimeout(_ => {
        if (setLoginIndex) {
            clearInterval(setLoginIndex)
            setLoginIndex = 0
        }
    }, 5000)
    awtgetuserinfo()?.then(async _ => {
        const getShopInfoFun: any = await getShopInfoData()
        // console.log('getShopInfoFun', getShopInfoFun)
        if (!getShopInfoFun.code) {
            console.log('获取店铺信息', getShopInfoFun)

            store.dispatch(setShopInfo(getShopInfoFun.data))
            store.dispatch(getAutoSetCounterListStor(getShopInfoFun.data))
            window.postMessage({
                type: 'dly_activity_modole',
                dly_method: 'doudian_user_loginstatus',
                shop_id: store.getState().jinritemaiShops.userShopId,
            })
            store.dispatch(getProductList(''))
            store.dispatch(getDlyUserBindInfos(''))
            loginShopGetShopInfos()
            addNewConter()
        }
        getAutoActivityListStor() // 获取活动信息列表
        store.dispatch(setIsInit(true))
        getOssKeyData()
        // 获取请求参数
        store.dispatch(setToken())
        // // 获取限时限量购活动商品列表
        store.dispatch(getGoodsAsync(''))
        // // 获取满减活动商品列表
        store.dispatch(getDiscountActivity(''))
        // 获取满减同品多件
        store.dispatch(getDiscountOnePrdActivity(''))
        // 获取新人礼金商品列表
        store.dispatch(getDetailNewUserGiftAllData(''))
        // // 惊喜券
        store.dispatch(getSurpriseTicketLisAllData(''))
        // // 优惠券
        store.dispatch(getCouponListAllData(''))
        // // 运费模板
        store.dispatch(getshopFreightTemplateAsync(''))
        // // 拼团
        // store.dispatch(getPromotionListFlashAllData(''))
        // // 获取手机号
        store.dispatch(getUserShopID(''))
    }).catch(err => {
        console.log('登录信息异常', err)
    })


}
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
localStorage.removeItem('wtshopuser')
initPageInfo()

